﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly HttpClient _client;
        public MainWindow()
        {
            InitializeComponent();
         
            _client = new HttpClient();
            _client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
        }
        private async Task<string> DownloadHtmlAsync(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                // 设置User-Agent避免被服务器拒绝
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");
                return await client.GetStringAsync(url);
            }
        }
       
        private async void btnFetch_Click(object sender, RoutedEventArgs e)
        {
            var keyword = txtUrl.Text.Trim();
            if (string.IsNullOrEmpty(keyword)) return;

            try
            {
                var html = await SearchBaiduAsync(keyword);
                var cleanText = ExtractVisibleText(html);
                string result = TruncateText(cleanText, 200);

                // 异步更新两个文本框 
                Text1.Text = result.Substring(0,100);
                Text2.Text = result.Substring(100,100); 
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误: {ex.Message}");
            }
            
        }
        private async Task<string> SearchBaiduAsync(string keyword)
        {
            var encoded = Uri.EscapeDataString(keyword);
            var response = await _client.GetAsync($"https://www.baidu.com/s?wd={encoded}");
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
        private string ExtractVisibleText(string html)
        {
            // 移除脚本/样式标签 
            var cleaned = Regex.Replace(html, @"<(script|style)[^>]*>.*?</\1>", "", RegexOptions.Singleline);
            // 提取正文内容
            return Regex.Replace(cleaned, @"<[^>]+>| ", " ").Trim();
        }
       
        private string TruncateText(string text, int maxLength)
        {
          
            if (string.IsNullOrEmpty(text))
                return "[无有效内容]"; 
            if (maxLength <= 0)
                return "[参数错误]";   

            // 动态计算安全截取长度
            int safeLength = Math.Min(maxLength, text.Length);
            return text.Substring(0, safeLength) + (text.Length > maxLength ? "..." : "");
        }


        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
